<?php

class EasemobUserSeeder extends Seeder {

    public function run() 
    {
    	$easemobUserIds = $this->getEasemobUserIds();
    	
		$query = User::whereIn('id', $easemobUserIds);
		$count = $query->count();
    	
    	// 更新已有的环信用户的密码
		Log::info(sprintf("We are going to update passwords for %s easemob users", $count));

		$page_row = 30;
		$page = 1;
		$total_page = ceil($count/$page_row);

		try {
			$easemob = new Easemob();
			$num = 0;
			for($page=1;$page<=$total_page;$page++) {
				$offset = ($page-1)*$page_row;
				Log::info(sprintf("We are at page %s", $page));

				$users = $query->take($page_row)->skip($offset)->get();

				$userOptions = [];
				foreach($users as $user) {
					$password = Easemob::getPass($user);

					$easemobUser = array(
					    'username' => $user->id,
					    'newpassword' => $password,
					);
					
					if ($password) {
					    $result_re = $easemob->editPassword($easemobUser);

					    $dagg = json_decode($result_re);
					    
					    if(isset($dagg->error)) {
					        throw new Exception($dagg->error);
					    }
					    
						Log::info(sprintf("Successfully updated password for %s easemob user: %s", ++$num, json_encode($easemobUser)));
					} else {
					    Log::info(sprintf("No password found for user %s", json_encode($easemobUser)));
					}
				}
			}
		} catch(Exception $ex){
			Log::error($ex);
		}

    	// 找出还不是环信的用户，把他们注册到环信中去
		$query = User::whereNotIn('id', $easemobUserIds);
		$count = $query->count();

		Log::info(sprintf("We are going to register %s easemob users", $count));

		$page_row = 30;
		$page = 1;
		$total_page = ceil($count/$page_row);

		try {
			$easemob = new Easemob();
			$num = 0;
			for($page=1;$page<=$total_page;$page++) {
				$offset = ($page-1)*$page_row;
				Log::info(sprintf("We are at page %s", $page));

				$users = $query->take($page_row)->skip($offset)->get();

				$userOptions = [];
				foreach($users as $user) {
					$nickname = $user->nikname?$user->nikname:$user->realname;
					$password = Easemob::getPass($user);

					$easemobUser = array(
					    'username' => $user->id,
					    'password' => $password,
					    'nickname' => $nickname				//可不传
					);
					
					if ($password) {
						Log::info(sprintf("Successfully added %s easemob user: %s", ++$num, json_encode($easemobUser)));

						$userOptions[] = $easemobUser;
					} else {
					    Log::info(sprintf("No password found for user %s", json_encode($easemobUser)));
					}
				}
				
				Log::info(sprintf("user options %s", json_encode($userOptions)));

				if (!empty($userOptions)) {
    				$result_re = $easemob->accreditRegister($userOptions);
    				$dagg = json_decode($result_re);
    
    				if(isset($dagg->error)) {
    					throw new Exception($dagg->error);
    				}
				}
			}
		} catch(Exception $ex){
			Log::error($ex);
		}
    }

	/*
	 * 环信服务器用户
	 */
	public function getEasemobUserIds() 
	{
		$limit = '1000';
		$result = new Easemob();
		$easemob_id = array();
		$details = json_decode($result->userAll($limit), true);
		$data = $details['entities'];
		foreach($data as $key){
			$easemob_id[] = $key['username'];
		}
		while (isset($details['cursor'])) {
			$details = json_decode($result->userAll($limit, $details['cursor']), true);
			$data = $details['entities'];
			foreach($data as $key){
				$easemob_id[] = $key['username'];
			}
		}
		return $easemob_id;
	}
}